package com.tedu.jdbc;

import com.tedu.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created by IntelliJ IDEA.
 *
 * @author zq
 * @version 1.0
 * @create 2024-07-30 17:20
 * @since jdk1.8
 */
public class JDBCLogin02 {
    /*
    *登陆功能(使用预编译)
    *
    * */
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps= null;
        ResultSet rs = null;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();
        //获取连接
        try {
            conn = JDBCUtils.getConnection();
            //预编译sql语句
            String sql = "select nickname from userinfo where username = ? and password = ?";
            ps = conn.prepareStatement(sql);
            //设置参数
            ps.setString(1,username);
            ps.setString(2,password);
            //执行sql语句
            rs = ps.executeQuery();
            //处理结果
            if (rs.next()){
                System.out.println("登陆成功");
            }else {
                System.out.println("登陆失败");
            }
        }  catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtils.close(rs,ps,conn);
        }
    }
}
